package org.reliablesource.prism.core.internal;

import java.util.Observable;

import org.reliablesource.prism.core.IPrismAnnotation;
import org.reliablesource.prism.core.IPrismElement;
import org.reliablesource.prism.core.IPrismElementFactory;
import org.reliablesource.prism.core.IPrismModel;
import org.reliablesource.prism.core.IPrismScreen;
import org.reliablesource.prism.core.IPrismTutorial;
import org.reliablesource.prism.core.IPrismUpdateEvent;
import org.reliablesource.prism.core.plugin.PrismCorePlugin;

/**
 *PrismElementFactory.java<br>
 *	Created on: Feb 25, 2006<br><br>
 *
 *  @author Dennis Park 
 *  <a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 *	@since 1.0
 */

public class PrismElementFactory implements IPrismElementFactory {

	int elementNum = 0;
	
	public IPrismModel createModel(){
		return new PrismModel();
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.reliablesource.prism.core.IPrismElementFactory#createTutorialNode()
	 */
	public IPrismTutorial createTutorialNode() {
		PrismTutorial tutorial;
		if(PrismCorePlugin.getDefault().getModel().getChildListSize() < 1){
			tutorial = new PrismTutorial();
			tutorial.setName("Prism Capture Session");
			tutorial.setId(elementNum++);
		}else
			tutorial = (PrismTutorial) PrismCorePlugin.getDefault().getModel().getChild(0);
		return tutorial;
	}

	/*
	 * (non-Javadoc)
	 * @see org.reliablesource.prism.core.IPrismElementFactory#createScreen()
	 */
	public IPrismScreen createScreen() {
		PrismScreen scr = new PrismScreen();
		scr.setId(elementNum++);
		scr.setName("Screen " + scr.getId());
		IPrismTutorial tutorial;

		if (PrismCorePlugin.getDefault().getModel().getChildListSize() < 1){
			tutorial = createTutorialNode();
			PrismCorePlugin.getDefault().getModel().addChild(tutorial);
			IPrismUpdateEvent _updateEvent = PrismCorePlugin.getDefault().getElementFactory().createUpdateEvent(IPrismUpdateEvent.ADD);
			_updateEvent.addToChangedList(tutorial);
			((Observable)PrismCorePlugin.getDefault().getModel()).notifyObservers(_updateEvent);
		}
		else
			tutorial = (IPrismTutorial) PrismCorePlugin.getDefault().getModel().getChild(0);

		// Set the parent of this screen
		scr.setParent(tutorial);

		// Add screen to node as a child
		tutorial.addChild(scr);
		return scr;
	}
	
	
	/**
	 * Method is strictly used to test the element factory.
	 * 
	 * @deprecated
	 */
	public IPrismElement createTestTutorialNode() {

		PrismTutorial parentElement = new PrismTutorial();
		parentElement.setName("Main");
		for (int i = 0; i < 4; i++) {
			PrismTutorial element = new PrismTutorial();
			element.setName("TutorialNode:   " + i);

			for (int j = 0; j < 10; j++) {
				IPrismScreen child = createTestScreen();
				child.setName("  TutorialScreen:   " + j);
				element.addChild(child);
			}
			((IPrismTutorial)parentElement).addChild(element);
		}
		return parentElement;
	}

	/**
	 * Method is strictly used to test the element factory.
	 * @deprecated
	 * @return	IPrismScreen
	 */
	public IPrismScreen createTestScreen() {
		PrismScreen element = new PrismScreen();
		element.setId(elementNum++);
		element.setName("Screen " + element.getId());
		return element;
	}

	/*
	 * (non-Javadoc)
	 * @see org.reliablesource.prism.core.IPrismElementFactory#createPrismAnnotation(org.reliablesource.prism.core.IPrismScreen)
	 */
	public IPrismAnnotation createPrismAnnotation(IPrismScreen _parentScreen) {
		PrismAnnotation _newElement = new PrismAnnotation();
		_newElement.setParent(_parentScreen);
		_newElement.setId(elementNum++);
		_newElement.setName("Annotation " + _newElement.getId());
		return _newElement;
	}

	public IPrismUpdateEvent createUpdateEvent(int type) {
		return new PrismUpdateEvent(type);
	}
}